{% extends 'web/layout/basic.html' %}
{% load static %}

{% block title %}用户登录{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户登录</div>
        <form id="form" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="clearfix">
                            <div class="col-xs-7" style="padding-left: 0">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5" style="padding-right: 0"><input id="btnSms" type="button" class="btn btn-default col-md-12" value="点击获取验证码" ></div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div class="row">
                <div class="col-xs-7">
                    <input id="btnSubmit" type="button" class="btn btn-primary col-md-12" value="登  录">
{#                    <a class="col-md-5" href="{% url 'login' %}"><input type="button" class="btn btn-default" style="float: right" value="账号密码登录"></a>#}
                </div>
                <a class="col-xs-5 login-style" href="{% url 'login' %}">账号密码登录</a>
            </div>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        // 页面加载完成之后自动执行的函数
        $(function () {
            bindClickBtnSms();
            bindClickBtnSubmit();
        });

        // 按钮绑定点击事件
        function bindClickBtnSms() {
            $('#btnSms').click(function () {

                $('.error-msg').empty();

                // 获取用户输入的手机号
                let mobilePhone = $('#id_mobile_phone').val();

                // 发送Ajax请求，把手机号发过去
                $.ajax({
                    url: "{% url 'send_sms' %}", // 去urls文件中读取name='send_sms'的url，等价于 /send/sms/
                    type: "GET",
                    data: {
                        mobile_phone: mobilePhone,
                        template: "login",
                    },
                    dataType: "JSON", // 将服务端(HttpResponse)返回的数据反序列化为字典,JsonResponse不用反序列化
                    success: function (res) {
                        // ajax请求发送成功之后自动执行的函数, res就是后端返回的值
                        console.log(res);
                        if (res.status) {
                            sendSmsRemind();
                        } else {
                            $.each(res.error, function (k, v) {
                                $("#id_" + k).next().text(v[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindClickBtnSubmit() {
            $('#btnSubmit').click(function() {
                $('.error-msg').empty();

                let data = $('#form').serialize();   // 所有字段数据 + csrf token
                $.ajax({
                    url: "{% url 'login_sms' %}",
                    type: "POST",
                    data: data,
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res);
                        if (res.status) {
                            location.href = res.href;
                        } else {
                            $.each(res.error, function (k, v) {
                                $("#id_" + k).next().text(v[0]);
                            })
                        }
                    }
                })
            })
        }
        
        // 验证码按钮倒计时
        function sendSmsRemind() {
            let $btnSms = $('#btnSms');
            $btnSms.prop("disabled", true);
            let time = 60;
            let obj = setInterval(function () {
                $btnSms.val(time.toString() + '秒后重发');
                time = time - 1;
                if (time < 1) {
                    clearInterval(obj);
                    $btnSms.prop("disabled", false);
                    $btnSms.val('点击获取验证码');
                }
            }, 1000)
        }
    </script>
{% endblock %}